<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /* 实现一个 filter 函数 */

        let arr = [1,2,3,4]

        Array.prototype.myFilter =function  myFilter(fn, context){
            if(typeof fn !== 'function') {
                throw new TypeError(`${fn}不是一个函数`)
            }
            let arr = this
            // console.log("this", this)  //  this 就是 点前 的 数组
            let temp = []
            for(let i=0; i<arr.length; i++) {
                let result = fn.call(context, arr[i], i, arr)
                // result 返会的是 布尔值
                if(result) temp.push(arr[i])
            }
            return temp
        }

        console.log(arr.myFilter(i => i%2 === 0))
       
    </script>
</body>
</html>